<?php
require_once 'Models/Cms/Dao/CmsBaseDao.php';

class CmsCategoryDao extends CmsBaseDao {
	private static $tableName = 'cms_category';
	
	public function add($fields) {
		if (!is_array($fields)) return 0;
		return $this->queryHelper()->insert(self::$tableName, $fields);
	}
	
	public function get($id) {
		if ($id <= 0) return null;
		return $this->queryHelper()->get(self::$tableName, null, 'id=?', array($id));
	}
	
	public function getByName($name) {
		if ($name == '') return null;
		return $this->queryHelper()->get(self::$tableName, null, 'name=?', array($name));
	}
	
	public function update($id, $fields) {
		if (!is_array($fields) || !count($fields)) return 0;
		
		return $this->queryHelper()->update(self::$tableName, $fields, 'id=?', array($id));
	}
	
	public function delete($id) {
		return $this->queryHelper()->delete(self::$tableName, 'id=?', array($id));
	}
	
	public function findByParentIdWithOrder($parentId) {
		return $this->queryHelper()->query(self::$tableName, null, 'parent_id=?', array($parentId), null, null, 'order_number');
	}
	
	public function findAll() {
		return $this->queryHelper()->setQueryKey('id')->query(self::$tableName, null, null, null, null, null, 'parent_id, order_number');
	}

}
